<article id="wikiArticle">
<div> <div class="blockIndicator obsolete obsoleteHeader"><p><strong><span class="icon-only-inline" title="This is an obsolete API and is no longer guaranteed to work."><i class="icon-trash"> </i></span> 已废弃</strong><br/>This feature is obsolete. Although it may still work in some browsers, its use is discouraged since it could be removed at any time. Try to avoid using it.</p></div></div>
<h2 id="概述">概述</h2>
<p><strong><code>Object.observe()</code></strong> 方法用于异步地监视一个对象的修改。当对象属性被修改时，方法的回调函数会提供一个有序的修改流。然而，这个接口已经被废弃并从各浏览器中移除。你可以使用更通用的 <a href="Reference/Global_Objects/Proxy" title="Proxy 对象用于定义基本操作的自定义行为（如属性查找，赋值，枚举，函数调用等）。"><code>Proxy</code></a> 对象替代。</p>
<h2 id="语法">语法</h2>
<pre><code class="language-javascript"><code>Object.observe(<var>obj</var>, <var>callback</var></code>[, <var>acceptList</var>])</code></pre>
<h3 id="参数">参数</h3>
<dl>
<dt><code>obj</code></dt>
<dd>被监控的对象.</dd>
<dt><code>callback</code></dt>
<dd>当对象被修改时触发的回调函数，其参数为: 
 <dl>
<dt><code>changes</code></dt>
<dd>一个数组，其中包含的每一个对象代表一个修改行为。每个修改行为的对象包含: 
  <ul>
<li><strong><code>name</code></strong>: 被修改的属性名称<span style="font-family: consolas,monaco,andale mono,monospace;">。</span></li>
<li><strong><code>object</code></strong>: 修改后该对象的值<span style="font-family: consolas,monaco,andale mono,monospace;">。</span></li>
<li><strong><code>type</code></strong>: 表示对该对象做了何种类型的修改，可能的值为<code>"add"</code>, <code>"update"</code>, or <code>"delete"</code><span style="font-family: consolas,monaco,andale mono,monospace;">。</span></li>
<li><strong><code>oldValue</code></strong>: 对象修改前的值。该值只在<code>"update"<font face="Open Sans, sans-serif">与</font></code><code>"delete"有效。</code></li>
<li> </li>
</ul>
</dd>
<dt><font face="Consolas">acceptList</font></dt>
<dd>在给定对象上给定回调中要监视的变化类型列表。如果省略， <code><font face="Courier New">["add", "update", "delete", "reconfigure", "setPrototype", "preventExtensions"]</font></code> 将会被使用。</dd>
</dl>
</dd>
</dl>
<h2 id="描述">描述</h2>
<p><code style="font-style: normal;">callback</code> 函数会在<code>对象被改变时被调用，其参数为一个包含所有修改信息的有序的数组对象。</code></p>
<h2 id="例子">例子</h2>
<h3 id="例子_打印出三种不同操作类型的日志">例子: 打印出三种不同操作类型的日志</h3>
<pre><code  class="language-javascript">var obj = {
  foo: 0,
  bar: 1
};

Object.observe(obj, function(changes) {
  console.log(changes);
});

obj.baz = 2;
// [{name: 'baz', object: &lt;obj&gt;, type: 'add'}]

obj.foo = 'hello';
// [{name: 'foo', object: &lt;obj&gt;, type: 'update', oldValue: 0}]

delete obj.baz;
// [{name: 'baz', object: &lt;obj&gt;, type: 'delete', oldValue: 2}]
</code></pre>
<h3 id="例子_数据绑定">例子: 数据绑定</h3>
<pre><code  class="language-javascript">// 一个数据模型
var user = {
  id: 0,
  name: 'Brendan Eich',
  title: 'Mr.'
};

// 创建用户的greeting
function updateGreeting() {
  user.greeting = 'Hello, ' + user.title + ' ' + user.name + '!';
}
updateGreeting();

Object.observe(user, function(changes) {
  changes.forEach(function(change) {
    // 当name或title属性改变时, 更新greeting
    if (change.name === 'name' || change.name === 'title') {
      updateGreeting();
    }
  });
});
</code></pre>
<h2 id="Specifications" name="Specifications">规范</h2>
<p><a class="external" href="https://github.com/arv/ecmascript-object-observe" rel="noopener">Strawman proposal for ECMAScript 7</a>.</p>
<h2 id="Browser_compatibility" name="Browser_compatibility">浏览器兼容性</h2>
<div><div class="blockIndicator warning"><strong><a class="external" href="https://github.com/mdn/browser-compat-data" rel="noopener">We're converting our compatibility data into a machine-readable JSON format</a></strong>.
            This compatibility table still uses the old format,
            because we haven't yet converted the data it contains.
            <strong><a class="new" href="/zh-CN/docs/MDN/Contribute/Structures/Compatibility_tables" rel="nofollow">Find out how you can help!</a></strong></div>
<div class="htab">
<a id="AutoCompatibilityTable" name="AutoCompatibilityTable"></a>
<ul>
<li class="selected"><a>Desktop</a></li>
<li><a>Mobile</a></li>
</ul>
</div></div>
<div id="compat-desktop">
<table class="compat-table">
<tbody>
<tr>
<th>Feature</th>
<th>Chrome</th>
<th>Firefox (Gecko)</th>
<th>Internet Explorer</th>
<th>Opera</th>
<th>Safari</th>
</tr>
<tr>
<td>Basic support</td>
<td>36</td>
<td><span style="color: #f00;">未实现</span></td>
<td><span style="color: #f00;">未实现</span></td>
<td>23</td>
<td><span style="color: #f00;">未实现</span></td>
</tr>
</tbody>
</table>
</div>
<div id="compat-mobile">
<table class="compat-table">
<tbody>
<tr>
<th>Feature</th>
<th>Android</th>
<th>Chrome for Android</th>
<th>Firefox Mobile (Gecko)</th>
<th>IE Mobile</th>
<th>Opera Mobile</th>
<th>Safari Mobile</th>
</tr>
<tr>
<td>Basic support</td>
<td><span style="color: #f00;">未实现</span></td>
<td>36</td>
<td><span style="color: #f00;">未实现</span></td>
<td><span style="color: #f00;">未实现</span></td>
<td>23</td>
<td><span style="color: #f00;">未实现</span></td>
</tr>
</tbody>
</table>
</div>
<h2 id="See_also" name="See_also">相关链接</h2>
<ul>
<li><a href="Reference/Global_Objects/Object/unobserve" title="Object.unobserve() 是用来移除通过 Object.observe()设置的观察者的方法。"><code>Object.unobserve()</code></a> <span title="这是一个实验性的 API，请尽量不要在生产环境中使用它。"><i class="icon-beaker"> </i></span></li>
<li><a href="Reference/Global_Objects/Array/observe" title='Array.observe() 方法用于异步监视数组发生的变化，类似于针对对象的 Object.observe() 。当数组的值发生变化时，它按发生顺序提供了一个变化流。与 Object.observe() 类似，它由如下可接受的变化类型列表["add"、"update"、"delete"、"splice"]触发。'><code>Array.observe()</code></a> <span title="这是一个实验性的 API，请尽量不要在生产环境中使用它。"><i class="icon-beaker"> </i></span></li>
</ul>
</article>